//
//  LGExtendTimeView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/23.
//  延长时长View

#import "LGExtendTimeView.h"

@interface LGExtendTimeView()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *lengthLabel;
/**   */
@property (nonatomic , strong)UILabel *tipLabel;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIView *actionView;
/**   */
@property (nonatomic , strong)UIButton *minusBtn;
/**   */
@property (nonatomic , strong)UILabel *extentLabel;
/**   */
@property (nonatomic , strong)UIButton *addBtn;
/**   */
@property (nonatomic , assign)NSInteger count;

@end

@implementation LGExtendTimeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.count = 0;
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        [self creareSubView];
    }
    return self;
}

-(void)showViewAniamtion{
    self.hidden = NO;
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.frame = CGRectMake(0, Screen_H-height+15, Screen_W, height);
    }];
}

-(void)cancelButtonAction{
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, height);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

-(void)sureButtonAction{
    if(self.count>0){
        if(self.delegate && [self.delegate respondsToSelector:@selector(extendTimeAction:)]){
            [self.delegate extendTimeAction:[NSString stringWithFormat:@"%f",self.count*0.5]];
        }
        [self cancelButtonAction];
    }else{
        [TooltipView showMessage:@"时长不能为0" offset:0];
    }
}


-(void)setCount:(NSInteger)count{
    _count = count;
    self.extentLabel.text = [NSString stringWithFormat:@"%.1f 小时",count*0.5];
}

-(void)setSeconds:(NSInteger)seconds{
    _seconds = seconds;
    NSString *hour = [NSString stringWithFormat:@"%.2ld",seconds/3600];
    NSString *minite = [NSString stringWithFormat:@"%.2ld",(seconds-[hour integerValue]*3600)/60];
    NSString *second = [NSString stringWithFormat:@"%.2ld",seconds%60];
    self.timeLabel.text = [NSString stringWithFormat:@"%@:%@:%@",hour,minite,second];
}

//减
-(void)minusTimeAction{
    if(self.count > 0){
        self.count--;
    }else{
        [TooltipView showMessage:@"不能再减啦！" offset:0];
    }
}

//加
-(void)addTimeAction{
    if(self.count < 20){
        self.count++;
    }else{
        [TooltipView showMessage:@"不能再加啦！" offset:0];
    }
}



#pragma mark
#pragma mark ====> 创建控件
-(void)creareSubView{
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.cancelBtn];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.sureBtn];
    [self.baseView addSubview:self.lengthLabel];
    [self.baseView addSubview:self.tipLabel];
    [self.baseView addSubview:self.timeLabel];
    [self.baseView addSubview:self.actionView];
    [self.actionView addSubview:self.minusBtn];
    [self.actionView addSubview:self.extentLabel];
    [self.actionView addSubview:self.addBtn];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(22));
        make.centerY.equalTo(self.actionView);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tipLabel.mas_right).offset(3);
        make.centerY.equalTo(self.tipLabel);
    }];
    
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(170)+bottomSafeBarHeight)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(14, UIFontWeightSemibold) textColor:@"#999999" imageName:@""];
        _cancelBtn.frame = CGRectMake(viewPix(15), viewPix(15), viewPix(48), viewPix(30));
        [_cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" titleFont:LGFontWeight(14, UIFontWeightSemibold) textColor:@"#429BFF" imageName:@""];
        _sureBtn.frame = CGRectMake(Screen_W-viewPix(63), viewPix(15), viewPix(48), viewPix(30));
        [_sureBtn addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"延长咨询" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(100), viewPix(15), Screen_W-viewPix(200), viewPix(30));
    }
    return _titleLabel;
}

-(UILabel *)lengthLabel{
    if(!_lengthLabel){
        _lengthLabel = [UILabel lableWithText:@"时长" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _lengthLabel.frame = CGRectMake(viewPix(22), viewPix(66), viewPix(60), viewPix(22));
    }
    return _lengthLabel;
}

-(UILabel *)tipLabel{
    if(!_tipLabel){
        _tipLabel = [UILabel lableWithText:@"咨询时长剩余" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _tipLabel;
}


-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"咨询时长剩余" colorString:@"#3291FF" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _timeLabel;
}

-(UIView *)actionView{
    if(!_actionView){
        _actionView = [[UIView alloc] initWithFrame:CGRectMake(Screen_W-viewPix(148), viewPix(90), viewPix(132), viewPix(36))];
        _actionView.layer.borderColor = [UIColor colorWithString:@"#EEEEEE"].CGColor;
        _actionView.layer.borderWidth = 1;
        _actionView.cornerRidus = viewPix(18);
    }
    return _actionView;
}

    
-(UIButton *)minusBtn{
    if(!_minusBtn){
        _minusBtn = [UIButton buttonWithTitle:@"-" titleFont:LGFontWeight(16, UIFontWeightSemibold) textColor:@"#9595A0" imageName:@""];
        _minusBtn.frame = CGRectMake(0, 0, viewPix(36), viewPix(36));
        [_minusBtn addTarget:self action:@selector(minusTimeAction) forControlEvents:UIControlEventTouchUpInside];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(36)-1, 0, 1, viewPix(36))];
        lineView.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
        [_minusBtn addSubview:lineView];
    }
    return _minusBtn;
}

-(UILabel *)extentLabel{
    if(!_extentLabel){
        _extentLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(13) textAlignment:NSTextAlignmentCenter lines:1];
        _extentLabel.frame = CGRectMake(viewPix(36), 0, viewPix(60), viewPix(36));
    }
    return _extentLabel;
}

-(UIButton *)addBtn{
    if(!_addBtn){
        _addBtn = [UIButton buttonWithTitle:@"+" titleFont:LGFontWeight(16, UIFontWeightSemibold) textColor:@"#FFFFFF" imageName:@""];
        _addBtn.frame = CGRectMake(viewPix(96), 0, viewPix(36), viewPix(36));
        _addBtn.backgroundColor = [UIColor colorWithString:@"#3291FF"];
        [_addBtn addTarget:self action:@selector(addTimeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

@end
